﻿using CustomControls.PoptipCtr;
using ManagementTool.BaseViewModels;
using ManagementTool.DB.Control;
using ManagementTool.DB.Sql;
using ManagementTool.NetWorks;
using ManagementTool.PageDeclaration;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ManagementTool.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {

        #region cmd
        private DelegateCommand _CloseCommand;
        public DelegateCommand CloseCommand =>
            _CloseCommand ?? (_CloseCommand = new DelegateCommand(CloseCommandMethod));

        private DelegateCommand _MaxCommand;
        public DelegateCommand MaxCommand =>
            _MaxCommand ?? (_MaxCommand = new DelegateCommand(MaxCommandMethod));

        private DelegateCommand _MinCommand;
        public DelegateCommand MinCommand =>
            _MinCommand ?? (_MinCommand = new DelegateCommand(MinCommandMethod));

        #endregion

        public MainWindowViewModel(IContainerExtension container) : base(container)
        {

        }

        public override async void ExecuteLoadedCommand()
        {
            base.ExecuteLoadedCommand();
        
            ToTargetConfigPage(SystemResourceExten.Nav_IndexPageView);
        }



        public override void ExecuteUnLoadedCommand()
        {
            base.ExecuteUnLoadedCommand();
        }

        private void ToTargetConfigPage(string _PagName)
        {
            NavigationParameters itemParameter = new NavigationParameters();
            Application.Current?.Dispatcher.Invoke(() => {
                Region.RequestNavigate(SystemResourceExten.Nav_MainContent, _PagName, itemParameter);
            });
        }
        private bool InitMySql()
        {
            bool flg = true;
            try
            {
                string _str_sql_creat_DeviceInfo = InstanceBase<SqlsControl>.Instance.GetCreatDeviceInfoDBSql("DeviceInfo");
                int result_DeviceInfo = InstanceBase<MySqlDbControl>.Instance.ExeCuteCommonSql(_str_sql_creat_DeviceInfo);
                if (result_DeviceInfo == -1)
                {
                    MessageControl.Warning("创建表DeviceInfo失败!", "RootMessageTooken");
                }
                string _str_sql_creat_FileInfo = InstanceBase<SqlsControl>.Instance.GetCreatFileInfoDBSql("FileInfo");
                int result_FileInfo = InstanceBase<MySqlDbControl>.Instance.ExeCuteCommonSql(_str_sql_creat_FileInfo);
                if (result_FileInfo == -1)
                {
                    MessageControl.Warning("创建表FileInfo失败!", "RootMessageTooken");
                }
            }
            catch (Exception ex)
            {
                flg = false;
            }
            return flg;
        }
        private void MinCommandMethod()
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;

        }

        private void MaxCommandMethod()
        {
            Application.Current.MainWindow.WindowState = Application.Current.MainWindow.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        private  void CloseCommandMethod()
        {
            CommonStaticModel.IsQuiteApp = true;

            Application.Current.Shutdown();
            Console.WriteLine("退出软件");
        }

    }
}
